package com.example.demo.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

/**
 * @program: SpringAOP
 * @ClassName ExcelUtil
 * @description:
 * @author: zsn
 * @create: 2024-07-09 09:17
 * @Version 1.0
 **/
public class ExcelUtil {

    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式

        // 声明列对象
        HSSFCell cell = null;

        List<Integer> list = new ArrayList<>(title.length);
        // 创建标题
        for (int i = 0; i < title.length; i++) {
            int length = title[i].length();
//            if (title[i].contains("（")){
//                length = length + 1;
//            }
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
            list.add(length);
        }

        // 创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                // 将内容按顺序赋给对应的列对象
                HSSFCell rowCell = row.createCell(j);
                rowCell.setCellValue(values[i][j]);
                rowCell.setCellStyle(style);
                if (values[i][j] != null){
                    int length = values[i][j].length();
                    Integer len = list.get(j);
                    if (length > len){
                        list.set(j,length);
                    }
                }
            }
        }


        for (int i = 0; i <title.length ; i++) {

            int width = list.get(i)*256*2;
            sheet.setColumnWidth(i, width);

        }
//        for (int i = 0; i < values.length; i++) {
//            sheet.autoSizeColumn(i);
//        }
        return wb;
    }

    public static void main(String[] args) {
        String ncFilePath = "C:\\Users\\zsn\\Desktop\\20日和22日\\新建文件夹 (5)\\预测/NMC_HENAN_PRE_L_G005_20240719170000_H24_H1.NC";

        try (NetcdfFile ncfile = NetcdfFile.open(ncFilePath)) {
            for (Variable var : ncfile.getVariables()) {
                System.out.println(var.getShortName()); // 打印变量名
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}